<template>
  <div class="app-container">
    <div class="menu-container">
      <el-form :inline="true" style="float: left;">
        <!-- <el-button size="mini" type="primary" @click="createVideo()">添加视频</el-button> -->
        <!-- <el-button size="mini" type="primary" @click="batchPutOnShelves">批量上架</el-button>
        <el-button size="mini" type="warning" @click="batchPullOffShelves">批量下架</el-button> -->
      </el-form>
      <div style="clear:both"/>
    </div>
    <el-table v-loading="listLoading" :data="page.records" border fit highlight-current-row style="width: 100%" height="500">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="订单编号">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="showDetail(scope.row)">{{ scope.row.order_id }}</el-button>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="影片名称">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="订单价格">
        <template slot-scope="scope">
          <span>{{ scope.row.amount }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="用户昵称">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="项目经理昵称">
        <template slot-scope="scope">
          <span>{{ scope.row.manager_nickname }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="订单状态">
        <template slot-scope="scope">
          <span>{{ scope.row.order_status_desc }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="250">
        <template slot-scope="scope">
          <el-button v-if="scope.row.order_status == 9 || scope.row.order_status == 10" type="text" size="small" @click="showRemark(scope.row)">退款理由</el-button>
          <el-button type="text" size="small" @click="modifyManager(scope.row)">更改项目经理</el-button>
          <el-button type="text" size="small" @click="modifyOrderStatus(scope.row)">修改订单状态</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="page.recordsTotal>0" :total="page.recordsTotal" :page.sync="listQuery.pageNum" :page-sizes="[20, 50, 100]" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog :visible.sync="dialogFormVisible" :title="dialogTitle" log- width="80%">
      <order-detail :is-edit="isEdit" :current-id="currentOrderId"/>
    </el-dialog>

    <el-dialog :visible.sync="dialogManagerFormVisible" title="更新项目经理" width="500px">
      <el-select v-model="selectedManagerId" filterable placeholder="请选择">
        <el-option
          v-for="item in managerOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button type="primary" size="small" @click="updateManager()">确定更换</el-button>
    </el-dialog>

    <el-dialog :visible.sync="dialogOrderStatusFormVisible" title="更新订单状态" width="500px">
      <el-select v-model="currentOrderStatus" filterable placeholder="请选择">
        <el-option
          v-for="item in orderStatusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <el-button type="primary" size="small" @click="updateOrderStatus()">确定更换</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, updateOrder } from '@/api/orders'
import { fetchManagerOptions } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import OrderDetail from './components/OrderDetail'

export default {
  name: 'VideoList',
  components: { Pagination, OrderDetail },
  data() {
    return {
      dialogTitle: '新增视频',
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20
      },
      currentOrderStatus: undefined,
      dialogManagerFormVisible: false,
      dialogOrderStatusFormVisible: false,
      isEdit: false,
      selectedManagerId: undefined,
      currentOrder: undefined,
      currentOrderId: undefined,
      formLabelWidth: '100px',
      dialogFormVisible: false,
      managerOptions: [],
      orderStatusOptions: [
        { 'value': 0, 'label': '待付款' },
        { 'value': 1, 'label': '待派单' },
        { 'value': 2, 'label': '匹配团队中' },
        { 'value': 4, 'label': '待启动' },
        { 'value': 5, 'label': '编剧中' },
        { 'value': 6, 'label': '拍摄中' },
        { 'value': 7, 'label': '剪辑中' },
        { 'value': 8, 'label': '待验收' },
        { 'value': 3, 'label': '待评论' },
        { 'value': 9, 'label': '退款中' },
        { 'value': 10, 'label': '已退款' },
        { 'value': 11, 'label': '已完成' },
        { 'value': 12, 'label': '已取消' },
        { 'value': 18, 'label': '待平台确认收款' },
        { 'value': 99, 'label': '购物车' }
      ],
      stateOptions: [{ value: 0, label: '正常' }, { value: 1, label: '禁用' }],
      categoryForm: { 'id': undefined, 'name': '', 'show_order': 1, 'state': 0 },
      page: { pageNum: 1, pageSize: 100, pageTotal: 0, recordsTotal: 0, records: [] }
    }
  },
  created() {
    this.getList()
    this.getManagerOptions()
  },
  methods: {
    showDetail(order) {
      this.dialogTitle = '订单详情'
      this.isEdit = true
      this.currentOrderId = order.id
      this.dialogFormVisible = true
    },
    modifyManager(order) {
      this.currentOrder = order
      this.selectedManagerId = order.manager_id
      this.dialogManagerFormVisible = true
    },
    modifyManagerSuccess() {
      this.currentOrder = undefined
      this.selectedManagerId = undefined
      this.dialogManagerFormVisible = false
    },
    showRemark(order) {
      this.$alert(order.back_remark, '退款理由', {
        confirmButtonText: '确定退款',
        callback: action => {
          const params = { id: order.id, order_status: 10 }
          this._updateOrder(params)
        }
      })
    },
    modifyOrderStatus(order) {
      this.currentOrderStatus = order.order_status
      this.currentOrder = order
      this.dialogOrderStatusFormVisible = true
    },
    modifyOrderStatusSuccess() {
      this.currentOrderStatus = undefined
      this.currentOrder = undefined
      this.dialogOrderStatusFormVisible = false
    },
    updateManager() {
      if (this.selectedManagerId === undefined) {
        return
      }
      const params = { id: this.currentOrder.id, manager_id: this.selectedManagerId }
      this._updateOrder(params, this.modifyManagerSuccess)
    },
    updateOrderStatus() {
      if (this.currentOrderStatus === undefined) {
        return
      }
      const params = { id: this.currentOrder.id, order_status: this.currentOrderStatus }
      this._updateOrder(params, this.modifyOrderStatusSuccess)
    },
    _updateOrder(params, callback) {
      updateOrder(params).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
          return
        }
        this.$message({
          type: 'success',
          message: '修改成功'
        })
        if (callback !== undefined) {
          callback()
        }
        this.getList()
      })
    },
    batchPutOnShelves() {
      var desc = '是否将所有选中视频上架'
      var params = { 'state': 0 }
      this._doSetBase(params, desc)
    },
    batchPullOffShelves() {
      var desc = '是否将所有选中视频下架'
      var params = { 'state': 1 }
      this._doSetBase(params, desc)
    },
    _doSetBase(params, desc, cancelMsg) {
      if (!this.selectedVideos || this.selectedVideos.length <= 0) {
        this.$message({
          message: '至少选择一个视频',
          type: 'error'
        })
        return
      }
      params['videoIds'] = this._getVideoIds(this.selectedVideos)
      if (!desc) {
        desc = '批量更新视频状态, 是否继续?'
      }
      if (!cancelMsg) {
        cancelMsg = '已取消操作'
      }
      this.myConfirm(desc, this._updateVideo, params, cancelMsg)
    },
    _updateVideo(params) {
      this.axios.post('/api/video/batch-update', params).then(response => {
        const r = response.data
        if (r.code !== 0) {
          this.$message({
            message: r.msg,
            type: 'error'
          })
          return
        }

        this.selectedGoods = []
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.list()
      })
    },
    createVideo() {
      this.isEdit = false
      this.currentOrderId = undefined
      this.dialogTitle = '添加视频'
      this.dialogFormVisible = true
    },
    modifyVideo(video) {
      this.isEdit = true
      this.currentOrderId = video.id
      this.dialogTitle = '编辑视频'
      this.dialogFormVisible = true
    },
    getManagerOptions() {
      fetchManagerOptions({}).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.managerOptions = data.data.managers
      })
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.page = data.data
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
